const fs = require('fs')
const path = require('path')

// 写入流
let data = 'hello world!!!!!!!!!!!!!!!!!'
let writeStream = fs.createWriteStream(path.join(__dirname, 'newMessage2.txt'))
writeStream.write(data, 'utf-8')
writeStream.end()
writeStream.on('finish', () => {
  console.log('写入完成')
  writeStream.close()
  console.log('写入流关闭')
})

// 读取流
// let fileReadStream = fs.createReadStream(path.join(__dirname, 'newMessage.txt'))
// let count = 0
// let str = ''
// fileReadStream.on('data', (chunk) => {
//   console.log(`${++count} ${chunk.toString()}`)
//   str += chunk
// })
// fileReadStream.on('end', () => {
//   console.log('读取结束')
//   console.log(count)
//   console.log(str)
// })
// // 读取失败
// fileReadStream.on('error', (error) => {
//   console.log(error);
// })
